local bushi = fk.CreateSkill {

  name = "joy__bushi",

  tags = {  },

}



bushi:addEffect(fk.Damaged, {
  name = "joy__bushi",
  anim_type = "masochism",
  events = {fk.Damaged},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(bushi.name) and player:hasSkill("joy__yishe") and (target == player or data.from == player) and #player:getPile("joy__zhanglu_mi") > 0 and not (data.from.dead or data.to.dead)
  end,
  on_trigger = function(self, event, target, player, data)
    if player:hasSkill(bushi.name) then
      self.cancel_cost = false
      for i = 1, data.damage do
        if #player:getPile("joy__zhanglu_mi") == 0 or self.cancel_cost then return end
        self:doCost(event, target, player, data)
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local x
    if target == player then
      x = "#joy__bushi-get"
    else 
      x = "#joy__bushi-give::"..target.id
    end
    if player.room:askForSkillInvoke(player, bushi.name, data,x) then
      return true
    end
    self.cancel_cost = true
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if #player:getPile("joy__zhanglu_mi") == 1 then
      room:obtainCard(target, player:getPile("joy__zhanglu_mi")[1], true, fk.ReasonPrey)
    else
      room:fillAG(player, player:getPile("joy__zhanglu_mi"))
      local id = room:askForAG(player, player:getPile("joy__zhanglu_mi"), false, bushi.name)
      room:closeAG(player)
      room:obtainCard(target, id, true, fk.ReasonPrey)
    end
  end,
})

return bushi